<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-button
        v-hasPermi="['system:sql:edit']"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="addHandle"
      >{{ $t('新增') }}</el-button>
      <el-button size="mini" type="danger" :disabled="btnDis" icon="el-icon-minus" @click="deleteHandle">删除</el-button>
    </el-row>

    <el-table
      v-loading="loading"
      :data="onlCgformEnhanceSqlList"
      tooltip-effect="darkTable"
      :row-style="{height: '50px'}"
      :cell-style="{padding: '3px'}"
      @row-click="editRow"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" align="center" width="55" />
      <el-table-column :label="$t('按钮编码')" align="center" prop="buttonCode" />
      <el-table-column :label="$t('SQL')" align="center" prop="sqlScript" />
      <el-table-column :label="$t('备注')" align="center" prop="remark" />
      <!-- <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:sql:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['system:sql:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->
    <!-- 添加或修改sql增强脚本对话框 -->
    <el-dialog
      top="20px"
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="40%"
      append-to-body
    >
      <onlCgformEnhanceSqlForm ref="onlCgformEnhanceSqlForm" />
    </el-dialog>
  </div>
</template>

<script>
import { getlistByFormId, delOnlCgformEnhanceSql, exportOnlCgformEnhanceSql } from '@/api/system/desform/enhance/onlCgformEnhanceSql'
import onlCgformEnhanceSqlForm from './onlCgformEnhanceSqlForm.vue'
import { mapState } from 'vuex'
export default {
    components: {
        onlCgformEnhanceSqlForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // sql增强脚本表格数据
          onlCgformEnhanceSqlList: [],
          // 弹出层标题
          title: '编辑',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              buttonCode: undefined,
              sqlName: undefined,
              sqlScript: undefined,
              formHeadId: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          headId: '',
          multipleTable: [],
          btnDis: true
      }
  },
  computed: {
    ...mapState('dbAbout', ['head'])
  },
  created() {
    if (this.head) {
      this.getList()
    }
  },
  methods: {
    /** 查询sql增强脚本列表 */
    getList() {
        this.loading = true
        getlistByFormId(this.head.id).then(response => {
            this.onlCgformEnhanceSqlList = response.data
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },
    init(row) {
      if (row) {
        this.headId = row.id
        console.log(this.headId)
        this.getList()
      }
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            buttonCode: undefined,
            sqlName: undefined,
            sqlScript: undefined,
            remark: undefined,
            formHeadId: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.onlCgformEnhanceSqlForm.init(null)
            this.$refs.onlCgformEnhanceSqlForm.initHeadId(this.headId)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.onlCgformEnhanceSqlForm.init(row)
            this.$refs.onlCgformEnhanceSqlForm.initHeadId(this.headId)
        })
    },
    /** 删除按钮操作 */
    deleteHandle() {
      if (this.multipleTable.length === 1) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delOnlCgformEnhanceSql(this.multipleTable[0].id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
      } else {
        this.$message({ message: '至多选择一条数据!', type: 'warning' })
      }
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportOnlCgformEnhanceSql(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    editRow(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.onlCgformEnhanceSqlForm.init(row)
      })
    },
    selectionChangeHandle(val) {
       this.multipleTable = val
       this.btnDis = !(val.length > 0)
    }
  }
}
</script>
